package com.androidhive.jsonparsing;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.vg.config.linkUrl;
import com.vg.leftmenu.Tao_Album;
import com.vg.leftmenu.banbe;
import com.vg.leftmenu.chinhsuathongtin;
import com.vg.leftmenu.contact;
import com.vg.leftmenu.timban;
import com.vg.library.Httppostaux;
import com.vg.loadimage.ImageLoader;
import com.vg.service.ServiceCity;
import com.vg.service.ServiceGender;
import com.vg.service.ServiceMaritalStatus;
import com.vg.service.ServiceUserType;
import com.vg.spinner.City;
import com.vg.spinner.Student;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.AdapterView.OnItemSelectedListener;

public class ActivityInfomations extends Activity {
	MyAdapter adapter;
	JSONParser jparse;
	JSONArray jsa;
	linkUrl mUrl;

	private ArrayList<Student> array;

	String strBody, strCity, strColor, strDrinker, strEducation, strGender,
			strMaritalStt, strHeight, strWeight, strReligion, strSmoker,
			strUserType;

	private static final String PHRASE = "Item_";
	private String prefCity = "PrefCity";
	private String prefGender = "PrefGender";
	private String prefMaritalStatus = "PrefMaritalStatus";
	private String prefUsertype = "PrefUsertype";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.swicher_right_info);

		Button btnUser, btnInfomation, btnCreateAlbum, btnContact, btnFriend, btnFindFriend, btnSearch;
		Spinner spCity, spMaritalStatus, spGender, spUserType;
		ImageView imgAvatar;
		mUrl = new linkUrl();

		btnUser = (Button) findViewById(R.id.bt_username);
		btnInfomation = (Button) findViewById(R.id.bt_thongtincanhan);
		btnCreateAlbum = (Button) findViewById(R.id.bt_album);
		btnContact = (Button) findViewById(R.id.bt_danhba);
		btnFriend = (Button) findViewById(R.id.bt_banbe);
		btnFindFriend = (Button) findViewById(R.id.bt_timban);
		btnSearch = (Button) findViewById(R.id.bt_timkiem);

		spGender = (Spinner) findViewById(R.id.spinner_chongioitinh);
		spCity = (Spinner) findViewById(R.id.spinner_noisong);
		spUserType = (Spinner) findViewById(R.id.spinner_khongrotinhtrang);
		spMaritalStatus = (Spinner) findViewById(R.id.spinner_tinhtranghonnhan);

		imgAvatar = (ImageView) findViewById(R.id.img_user);

		btnSearch.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});

		btnInfomation.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(ActivityInfomations.this,
						chinhsuathongtin.class);
//				i.putExtra("id", id_user);
				startActivity(i);

			}
		});

		btnCreateAlbum.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(ActivityInfomations.this, Tao_Album.class);
				startActivity(i);
			}
		});

		btnContact.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(ActivityInfomations.this, contact.class);
				startActivity(i);
			}
		});

		btnFriend.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(ActivityInfomations.this, banbe.class);
				startActivity(i);
			}
		});

		btnFindFriend.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(ActivityInfomations.this, timban.class);
				startActivity(i);
			}
		});

		
		dropItems(spCity, prefCity, dropSpinner(prefCity));
		dropItems(spGender, prefGender, dropSpinner(prefGender));
		dropItems(spMaritalStatus, prefMaritalStatus, dropSpinner(prefMaritalStatus));
		dropItems(spUserType, prefUsertype, dropSpinner(prefUsertype));
		
		spCity.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
//				pos_city = "" + spinner_city.getSelectedItemPosition();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		spGender.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
//				pos_gioitinh = "" + spinner.getSelectedItemPosition();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		spUserType.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
//				pos_khongro = "" + spinner_khongro.getSelectedItemPosition();

			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		spMaritalStatus.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
//				pos_honnhan = "" + spinner_tinhtrang.getSelectedItemPosition();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

//	public void putPref(String pref, String url) {
//		jparse = new JSONParser();
//		jsa = jparse.getJsonarray(url);
//		SharedPreferences prefs;
//		prefs = getSharedPreferences(pref, MODE_PRIVATE);
//		SharedPreferences.Editor editor = prefs.edit();
//		for (int i = 0; i < jsa.length(); i++) {
//			try {
//				editor.putString(PHRASE + i + "", jsa.getString(i));
//			} catch (JSONException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		editor.commit();
//	}

	public void dropItems(Spinner sp, String pref, ArrayList<Student> arr) {
		array = new ArrayList<Student>();
		adapter = new MyAdapter(this, R.layout.drop_gender, arr, true);
		sp.setAdapter(adapter);
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		for (int i = 0; i < prefs.getAll().size(); i++) {
			Student n = new Student(prefs.getString(PHRASE + i, ""));
			array.add(n);
		}
		adapter.notifyDataSetChanged();
	}

	public ArrayList<Student> dropSpinner(String pref) {
		ArrayList<Student> arrayList = new ArrayList<Student>();
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		for (int i = 0; i < prefs.getAll().size(); i++) {
			Student n = new Student(prefs.getString(PHRASE + i + "", ""));
			arrayList.add(n);
		}
		return arrayList;
	}

	public void test() {
		// int logstatus = -1;

		/*
		 * Creamos un ArrayList del tipo nombre valor para agregar los datos
		 * recibidos por los parametros anteriores y enviarlo mediante POST a
		 * nuestro sistema para relizar la validacion
		 */
		ArrayList<NameValuePair> postparameters2send = new ArrayList<NameValuePair>();

		postparameters2send.add(new BasicNameValuePair("email",
				"traito9x@gmail.com"));
		Httppostaux post = null;
		JSONArray jdata = post.getserverdata(postparameters2send,
				"http://deduocyeu.com/json/friend_list.php");

		Log.i("log_tag:", "" + jdata.toString());
		SystemClock.sleep(950);

		// si lo que obtuvimos no es null
		for (int i = 0; i <= jdata.length(); i++) {
			// Log.i("log_tag_jdata:",""+jdata.toString());
			try {
				JSONObject obj = jdata.getJSONObject(i);
				String id = obj.getString("use_id");
				Log.i("log_tag_jdata:", id);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}
